
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.Events;

public class ActionBar: MonoBehaviour
{
    [SerializeField] private Image m_BackgroundImage; // 背景图片
    [SerializeField] private ActionButton m_ActionButtonPrefab; // 按钮预制体

    private Color m_OriginalBackgroundColor; // 原始背景颜色
    private List<ActionButton> m_ActionButtons = new(); // 按钮列表

    void Awake()
    {
        m_OriginalBackgroundColor = m_BackgroundImage.color; // 记录原始背景颜色
        // Hide(); // 默认隐藏,游戏管理器中已经隐藏了
    }

    // 注册动作按钮
    public void RegisterAction(Sprite icon, UnityAction action)
    {
        // 实例化动作按钮预制体，并将其添加到当前对象的子对象中
        var actionButton = Instantiate(m_ActionButtonPrefab, transform);
        actionButton.Init(icon, action);
        // 将实例化的动作按钮添加到动作按钮列表中
        m_ActionButtons.Add(actionButton);
    }

    public void ClearActions()
    {
        for (int i = m_ActionButtons.Count - 1; i >= 0; i--)
        {
            Destroy(m_ActionButtons[i].gameObject); // 销毁动作按钮
            m_ActionButtons.RemoveAt(i); // 从动作按钮列表中移除
        }
    }

    public void Show()
    {
        m_BackgroundImage.color = m_OriginalBackgroundColor;
    }

    public void Hide()
    {
        m_BackgroundImage.color = new Color(0, 0, 0, 0); //黑色，隐藏时背景颜色为透明
    }
}